<template>
	<el-card class="layout-query">
		<el-form ref="queryRef" :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item label="关键词" prop="keyword">
				<el-input v-model="state.keyword" placeholder="请输入" clearable>
					<template #prepend>
						<el-select v-model="state.keyType" placeholder="请选择" style="width: 100px; min-width: 100px">
							<el-option label="标准编号" value="1" />
							<el-option label="标准名称" value="2" />
						</el-select>
					</template>
				</el-input>
			</el-form-item>
			<el-form-item>
				<el-button icon="Search" type="primary" @click="search()">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button icon="RefreshRight" @click="reset()">重置</el-button>
			</el-form-item>
		</el-form>
	</el-card>
	<el-card>
		<el-form :inline="true" style="text-align: right" class="anniu">
			<!-- <el-form-item v-auth="'M003_3_AnalysisChart'">
				<el-button type="primary" @click="fenxiDialogVisible = true">分析图 </el-button>
			</el-form-item> -->
			<el-form-item v-auth="'M003_3_Export'">
				<el-button type="primary" @click="exportHandle()">导出ecxcel </el-button>
			</el-form-item>
		</el-form>
		<el-table v-loading="state.dataListLoading" :data="dataList" stripe border @selection-change="selectionChangeHandle">
			<!-- <el-table-column type="selection" header-align="center" align="center" width="55" /> -->
			<el-table-column prop="index" label="序号" header-align="center" align="center" width="60" fixed="left"></el-table-column>
			<el-table-column prop="standardNumber" label="标准编号" header-align="center" align="center" width="140" resizable sortable></el-table-column>
			<el-table-column
				prop="standardProjectName"
				label="标准名称"
				header-align="center"
				align="center"
				width="120"
				fixed="left"
				resizable
				sortable
			></el-table-column>
			<el-table-column
				prop="implementationTime"
				label="实施日期"
				header-align="center"
				align="center"
				width="110"
				resizable
				sortable
			></el-table-column>
			<el-table-column
				prop="responsibleOrgName"
				label="主管部门"
				header-align="center"
				align="center"
				width="130"
				resizable
				sortable
			></el-table-column>
			<el-table-column prop="editorUnitName" label="主编单位" header-align="center" align="center" width="150" resizable sortable></el-table-column>
			<el-table-column
				prop="localStandardSuggestion"
				label="标准整合建议"
				header-align="center"
				align="center"
				width="200"
				show-overflow-tooltip
			></el-table-column>
			<el-table-column prop="editorUnitChange" label="主编单位变更情况" header-align="center" align="center" width="100" resizable sortable>
				<template #default="scope">
					{{ scope.row.editorUnitChange == 1 ? '有变更' : '未变更' }}
				</template>
			</el-table-column>
			<el-table-column prop="selfReviewConclusion" label="主编单位自审结果" header-align="center" align="center" width="120" resizable sortable>
				<template #default="scope">
					<span v-if="scope.row.selfReviewConclusion && scope.row.selfReviewConclusion > 1 && scope.row.selfReviewConclusion < 5">{{
						selfReviewConclusion.filter(item => item.code === scope.row.selfReviewConclusion)[0].name
					}}</span>
				</template>
			</el-table-column>
			<el-table-column prop="hasDocument" label="是否有管理文件、具体措施" header-align="center" align="center" width="120" resizable sortable>
				<template #default="scope">
					{{ scope.row.hasDocument == 1 ? '有' : '无' }}
				</template>
			</el-table-column>
			<el-table-column prop="clickCount" label="点击量" header-align="center" align="center" min-width="100" resizable sortable></el-table-column>
			<el-table-column
				prop="evaluationScore"
				label="发行评价(满分3)"
				header-align="center"
				align="center"
				min-width="100"
				resizable
				sortable
			></el-table-column>
			<el-table-column
				prop="propagationScore"
				label="宣贯评价(满分8)"
				header-align="center"
				align="center"
				min-width="100"
				resizable
				sortable
			></el-table-column>
			<el-table-column
				prop="articleScore"
				label="条文评价(满分5)"
				header-align="center"
				align="center"
				min-width="100"
				resizable
				sortable
			></el-table-column>
			<el-table-column
				prop="believeScore"
				label="标准采信(满分10)"
				header-align="center"
				align="center"
				min-width="100"
				resizable
				sortable
			></el-table-column>
			<el-table-column
				prop="totalScore"
				label="总分(满分26)"
				header-align="center"
				align="center"
				min-width="100"
				resizable
				sortable
			></el-table-column>
		</el-table>
		<el-pagination
			:current-page="state.pageNum"
			:page-size="state.pageSize"
			:total="state.total"
			layout="total, sizes, prev, pager, next, jumper"
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
		>
		</el-pagination>
		<fenxiDialog v-model="fenxiDialogVisible" />
	</el-card>
</template>
<script setup lang="ts">
import { getData1, getList } from '@/api/fushen/score'
import { useDictionaryStore } from '@/store/modules/dictionary'
import { useUserStore } from '@/store/modules/user'
import axios from 'axios'
import type { TableInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'
import fenxiDialog from '../components/fenxiDialog.vue'

const userStore = useUserStore()
const dictionaryStore = useDictionaryStore()

const promotersSession = dictionaryStore.getDictionaryByCode('T008')
const adoptTechnology = dictionaryStore.getDictionaryByCode('T009')
const winningAwards = dictionaryStore.getDictionaryByCode('T010')
const selfReviewConclusion = dictionaryStore.getDictionaryByCode('T0020')
const reviewResult = dictionaryStore.getDictionaryByCode('T016')
const state = reactive({
	queryForm: {
		standardNumber: '',
		standardProjectName: ''
	},
	pageNum: 1,
	pageSize: 10,
	total: 0,
	dataListLoading: false,
	keyType: '',
	keyword: null
})
const multipleTableRef = ref<TableInstance>()
const dataList = ref([])

const fenxiDialogVisible = ref(false)

const getDataList = () => {
	state.queryForm.standardNumber = ''
	state.queryForm.standardProjectName = ''
	if (state.keyword && !state.keyType) {
		ElMessage.error('请选择关键词类型')
		return
	}
	switch (state.keyType) {
		case '1':
			state.queryForm.standardNumber = state.keyword
			break
		case '2':
			state.queryForm.standardProjectName = state.keyword
			break
	}
	state.dataListLoading = true
	getList({
		pageIndex: state.pageNum,
		pageSize: state.pageSize,
		...state.queryForm
	}).then(res => {
		const list = res.data.list
		state.dataListLoading = false
		dataList.value = []
		if (list.length > 0) {
			list.forEach((item, index) => {
				dataList.value.push({
					index: index + 1,
					...item
				})
			})
		}
		state.total = res.data.total || 0
	})
}

const search = () => {
	state.pageNum = 1
	getDataList()
}

const reset = () => {
	state.queryForm.standardNumber = ''
	state.queryForm.standardProjectName = ''
	state.keyword = null
	state.keyType = ''
	state.pageNum = 1
	getDataList()
}

const echartsHandle = () => {}

const selectionChangeHandle = () => {}

const sizeChangeHandle = (e: number) => {
	state.pageSize = e
	state.pageNum = 1
	getDataList()
}
const currentChangeHandle = (e: number) => {
	state.pageNum = e
	getDataList()
}

const exportHandle = () => {
	try {
		// const selectedRows = multipleTableRef.value.getSelectionRows()
		// if (selectedRows.length == 0) {
		// 	throw new Error('请选择一个项目')
		// }
		// const ids = selectedRows.map(item => {
		// 	return item.id
		// })
		const param = {
			// processor: '',
			// projectId: ids.toString(),
			...state.queryForm
		}
		axios({
			method: 'post',
			url: window.location.protocol + '//' + window.location.host + '/JGBStandardWebApi' + '/api/review/exportReviewRating',
			headers: {
				'Content-Type': 'application/json;charset=UTF-8',
				Authorization: userStore.token
			},
			data: param,
			responseType: 'blob'
		})
			.then(response => {
				ElMessage.success('操作成功，文件已开始下载')
				const blob = new Blob([response.data], {
					type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
				})
				const url = window.URL.createObjectURL(blob)
				const a = document.createElement('a')
				a.href = url
				a.download = '标准复审评分导出文件.xls'
				a.click()
				window.URL.revokeObjectURL(url)
			})
			.catch(error => {
				console.error('导出失败:', error)
			})
	} catch (error) {
		console.error('提交处理失败:', error.message)
		ElMessage.error(error.message)
	}
}
onMounted(() => {
	getDataList()
	getData1().then(res => {
		console.log(res)
	})
})
</script>
<style lang="scss" scoped>
.anniu .el-form-item {
	margin-right: 10px !important;
}

.dialog-foot {
	display: flex;
	justify-content: center;
	padding-top: 20px;
	margin-bottom: 10px;
}

.tip {
	color: #f00;
	display: flex;
	align-items: center;
	padding-left: 100px;

	span {
		margin-left: 5px;
	}
}
</style>
